package com.ai.wxy.spring.user.controller;

import com.ai.spring.common.util.Page;
import com.ai.spring.common.util.ResponseResult;
import com.ai.spring.common.util.ResponseResultUtil;
import com.ai.wxy.spring.user.dto.UserDTO;
import com.ai.wxy.spring.user.query.UserQuery;
import com.ai.wxy.spring.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户接口
 *
 * @author 石头
 * @Date 2019/10/28
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/user")
public class UserRestApi {
    @Autowired
    private UserService userService;

    /**
     * 添加新用户
     * @param userDTO
     */
    @PostMapping(value = "/addUser")
    public ResponseResult<Long> addUser(@Validated(UserDTO.Addate.class) UserDTO userDTO){
        Long userId = userService.addUser(userDTO);
        return ResponseResultUtil.ofSuccess(userId);
    }

    /**
     * 修改新用户
     * @param userDTO
     */
    @PostMapping(value = "/updUser")
    public ResponseResult<Boolean> updUser(@Validated(UserDTO.Update.class) @Valid UserDTO userDTO){
        boolean result = userService.updUser(userDTO);
        return ResponseResultUtil.ofSuccess(result);
    }

    /**
     * 查询用户
     * @param query
     * @param page
     */
    @GetMapping(value = "/findUserByPage")
    public ResponseResult<Page<UserDTO>> findUserByPage(UserQuery query, Page<?> page){
        if (page.getCurrentPage() == null){
            page.setCurrentPage(0);
        }
        if (page.getPageSize() == null){
            page.setPageSize(10);
        }

        Page<UserDTO> result = userService.findUserByPage(query,page);
        return ResponseResultUtil.ofSuccess(result);
    }
}
